let marketList=localStorage.getItem("marketList");
let marketInfo = localStorage.getItem("marketInfo");
let marketTimeStamp=localStorage.getItem("marketTimeStamp");
let refresh=localStorage.getItem("refresh");

marketList=JSON.parse(marketList);

if(marketList==null){
  let {marketSwitchList} = require("../service/eth/config");
  marketList=[].concat(marketSwitchList)
  localStorage.setItem("marketList",JSON.stringify(marketList))
}

if(marketInfo){
  marketInfo = JSON.parse(marketInfo);
}else{
  marketInfo=[]
}

const state={
  marketInfo:marketInfo,
  marketList:marketList,
  marketTimeStamp:marketTimeStamp,
  refresh:refresh
}

const getters={
  marketInfo:state=>state.marketInfo,
  marketList:state=>state.marketList,
  marketTimeStamp:state=>state.marketTimeStamp,
  refresh:state=>state.refresh
}

const actions={
  setMarketInfo:(context,marketInfo)=>{
    context.commit('mSetMarketInfo',marketInfo)
    localStorage.setItem("marketInfo",JSON.stringify(marketInfo))
  },
  setMarketByIndex:(context,market,index)=>{
    context.commit('mSetMarketByIndex',market,index)
    localStorage.setItem("marketList",JSON.stringify(state.marketList))
  },
  setMarketList:(context,marketList)=>{
    context.commit('mSetMarketList',marketList)
    localStorage.setItem("marketList",JSON.stringify(marketList))
  },
  setMarketTimeStamp:(context,marketTimeStamp)=>{
    context.commit('mSetMarketTimeStamp',marketTimeStamp)
    localStorage.setItem("marketTimeStamp",marketTimeStamp)
  },
  setRefresh:(context,refresh)=>{
    context.commit('mSetRefresh',refresh)
    localStorage.setItem("refresh",refresh)
  }
}

const mutations={
  mSetMarketInfo:(state,marketInfo)=>{
    state.marketInfo = marketInfo;
  },
  mSetMarketByIndex:(state,market,index)=>{
    state.marketList[index] = market;
  },
  mSetMarketList:(state,marketList)=>{
    state.marketList = marketList;
  },
  mSetMarketTimeStamp:(state,marketTimeStamp)=>{
    state.marketTimeStamp = marketTimeStamp;
  },
  mSetRefresh:(state,refresh)=>{
    state.refresh = refresh;
  }
}

export default {
  state,getters,actions,mutations
}
